package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.vioao.wechat.api.component.ComponentApi;
import com.github.vioao.wechat.bean.response.component.GetComponentTokenResponse;
import one.stand.code.RedisCode;
import one.stand.service.CommontService;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author: QIK
 * @CreateDate: 2019/5/16 14:45
 */
@Service
public class CommontServiceImpl extends PublicBaseService implements CommontService {
    private ComponentApi componentApi = new ComponentApi();
    @Override
    public String getComponentToken() {
        String ticket = redisUtil.get(RedisCode.getTicket(componentConfig.getAppid()));
        String key = RedisCode.getComponentTokenKey(componentConfig.getAppid());
        String token = redisUtil.get(key);
        if (null == token) {
            GetComponentTokenResponse getComponentTokenResponse = componentApi.getComponentToken(componentConfig.getAppid(), componentConfig.getSecret(), ticket);
            if (getComponentTokenResponse.isSuccess()) {
                token = getComponentTokenResponse.getComponentAccessToken();
                redisUtil.set(key, getComponentTokenResponse.getComponentAccessToken(), 100, TimeUnit.MINUTES);
            }
            log.info("componentToken:{}", JSON.toJSONString(getComponentTokenResponse));
        }
        return token;
    }

}
